

-- Namespace
	local _,mod = ...


-- Object ref
	local RAID_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS


-- Constants
	local font = {mod.path..'media\\bavaria.ttf',8,'OUTLINEMONOCHROME'}
	
	local c_bar =    {.16,.16,.16}
	local c_barbg =  {.33,.33,.33}
	local c_border = {.07,.07,.07}
	
	local white = { r = 1, g = 1, b = 1 }


-- Helpers
	local function dim(color,perc)
		local r,g,b = color.r,color.g,color.b
		r,g,b = r*perc,g*perc,b*perc
		return r,g,b
	end
	
	local function abbrev(text)
		text = text:sub(1,1)
		text = text..'.'
		return text
	end


-- Base frame
	local target = mod:UF('target')
		target:SetFrameLevel(2)
		target:SetHeight(130) target:SetWidth(17)
		target:SetPoint('TOPRIGHT',UIParent,'CENTER',232,64)
		
		target:SetScript('OnEnter',function(self)
			GameTooltip_SetDefaultAnchor(GameTooltip,self)
			GameTooltip:SetUnit(self.unit)
		end)
		target:SetScript('OnLeave',function(self)
			GameTooltip:ClearLines()
			GameTooltip:Hide()
		end)
	
	local bg = target:CreateTexture(nil,'BACKGROUND')
		bg:SetAllPoints(true)
		bg:SetTexture(unpack(c_border))
		target.bg = bg


-- Health
	local hpbar = target:CreateTexture(nil,'ARTWORK')
		hpbar:SetHeight(128) hpbar:SetWidth(11)
		hpbar:SetPoint('TOPRIGHT',-1,-1)
		hpbar:SetTexture(unpack(c_bar))
		hpbar:Listen('health',function(self,min,max)
			local perc = min / max
			if perc <= 0 then
				perc = .001
			elseif perc > 1 then
				perc = 1
			end
			self:SetHeight(128*perc)
		end)
		target.hpbar = hpbar
	
	local inchealbar = target:CreateTexture(nil,'ARTWORK')
		inchealbar:SetHeight(128) inchealbar:SetWidth(11)
		inchealbar:SetPoint('TOP',hpbar,'BOTTOM')
		inchealbar:SetTexture(.2,.6,.2)
		inchealbar:Hide()
		inchealbar:Listen('incheal',function(self,heal,min,max)
			if heal then
				self:Show()
				local def = 1 - (min / max)
				local perc = heal / max
				if perc > def then
					perc = def
				end
				if perc <= 0 then
					perc = .001
				elseif perc > 1 then
					perc = 1
				end
				self:SetHeight(128*perc)
			else
				self:Hide()
			end
		end)
		target.inchealbar = inchealbar
	
	local hpbg = target:CreateTexture(nil,'BORDER')
		hpbg:SetHeight(128) hpbg:SetWidth(11)
		hpbg:SetPoint('TOPRIGHT',-1,-1)
		hpbg:SetTexture(unpack(c_barbg))
		target.hpbg = hpbg


-- Power
	local ppbar = target:CreateTexture(nil,'ARTWORK')
		ppbar:SetHeight(128) ppbar:SetWidth(3)
		ppbar:SetPoint('TOPLEFT',1,-1)
		ppbar:SetTexture(1,1,1)
		ppbar:Listen('power',function(self,min,max)
			local perc = min / max
			if perc <= 0 then
				perc = .001
			elseif perc > 1 then
				perc = 1
			end
			self:SetHeight(128*perc)
		end)
		ppbar:Listen('class',function(self,_,enclass)
			local color = RAID_CLASS_COLORS[enclass] or white
			self:SetTexture(dim(color,.75))
		end)
		target.ppbar = ppbar
	
	local ppbg = target:CreateTexture(nil,'BORDER')
		ppbg:SetHeight(128) ppbg:SetWidth(3)
		ppbg:SetPoint('TOPLEFT',1,-1)
		ppbg:SetTexture(unpack(c_barbg))
		target.ppbg = ppbg


-- Info
	local leveltext = target:CreateFontString(nil,'ARTWORK')
		leveltext:SetPoint('TOPLEFT',target,'TOPRIGHT',4,-15)
		leveltext:SetFont(unpack(font))
		leveltext:SetJustifyH('LEFT')
		leveltext:Listen('level',function(self,level,clas)
			if not level then
				level = '??'
			end
			clas = (
				clas == 'elite' and '+' or
				clas == 'rare' and 'rare' or
				clas == 'rareelite' and '+ rare' or
				clas == 'worldboss' and 'boss' or
				nil
			)
			if clas then
				level = level..' |cffffffff'..clas..'|r'
			end
			self:SetText(level)
		end)
		leveltext:Listen('class',function(self,_,enclass)
			local color = RAID_CLASS_COLORS[enclass] or white
			self:SetTextColor(color.r,color.g,color.b)
		end)
		target.leveltext = leveltext
	
	local nametext = target:CreateFontString(nil,'ARTWORK')
		nametext:SetPoint('TOPLEFT',target,'TOPRIGHT',4,-4)
		nametext:SetWidth(72) nametext:SetHeight(8)
		nametext:SetFont(unpack(font))
		nametext:SetJustifyH('LEFT')
		nametext:Listen('name',function(self,name,realm)
			name = name:gsub('(%S+) ',abbrev)
			if realm then
				name = name..' (*)'
			end
			self:SetText(name)
		end)
		nametext:Listen('class',function(self,_,enclass)
			local color = RAID_CLASS_COLORS[enclass] or white
			self:SetTextColor(color.r,color.g,color.b)
		end)
		target.nametext = nametext


-- Casting
	local castbar = target:CreateTexture(nil,'OVERLAY')
		castbar:SetHeight(128) castbar:SetWidth(3)
		castbar:SetPoint('TOPRIGHT',target,'TOPLEFT',-5,-1)
		castbar:Hide()
		castbar:SetTexture(unpack(c_bar))
		castbar.anim = function(elapsed)
			local self = castbar
			local min,max = self.min,self.max
			min = min + elapsed; self.min = min
			local perc = min / max
			if self.channel then
				perc = 1 - perc
			end
			if perc <= 0 then
				perc = .001
			elseif perc > 1 then
				perc = 1
			end
			self:SetHeight(perc*128)
		end
		castbar:Listen('casting',function(self,name,_,startT,endT,_,channel)
			if name then
				self:Show()
				self.min = GetTime() - startT
				self.max = endT - startT
				self.channel = channel
				mod:anim_register(self.anim)(0)
			else
				self:Hide()
				self.min = nil
				self.max = nil
				self.channel = nil
				mod:anim_unregister(self.anim)
			end
		end)
		castbar:Listen('class',function(self,_,enclass)
			local color = RAID_CLASS_COLORS[enclass] or white
			self:SetTexture(dim(color,.75))
		end)
		target.castbar = castbar
	
	local castbarbg = target:CreateTexture(nil,'BORDER')
		castbarbg:SetHeight(128) castbarbg:SetWidth(3)
		castbarbg:SetPoint('TOPRIGHT',target,'TOPLEFT',-5,-1)
		castbarbg:Hide()
		castbarbg:SetTexture(unpack(c_barbg))
		castbarbg:Listen('casting',function(self,name)
			if name then
				self:Show()
			else
				self:Hide()
			end
		end)
		target.castbarbg = castbarbg
	
	local castbarborder = target:CreateTexture(nil,'BACKGROUND')
		castbarborder:SetHeight(130) castbarborder:SetWidth(5)
		castbarborder:SetPoint('TOPRIGHT',target,'TOPLEFT',-4,0)
		castbarborder:Hide()
		castbarborder:SetTexture(unpack(c_border))
		castbarborder:Listen('casting',function(self,name)
			if name then
				self:Show()
			else
				self:Hide()
			end
		end)
		target.castbarborder = castbarborder
	
	local castname = target:CreateFontString(nil,'OVERLAY')
		castname:SetPoint('BOTTOMLEFT',castbarborder,'TOPLEFT',-4,4)
		castname:SetFont(unpack(font))
		castname:SetJustifyH('LEFT')
		castname:Listen('casting',function(self,name)
			if not name then name = '' end
			self:SetText(name)
		end)
		target.castname = castname
	
	local casttime = target:CreateFontString(nil,'OVERLAY')
		casttime:SetPoint('BOTTOMRIGHT',castbarborder,'BOTTOMLEFT',-1,1)
		casttime:SetFont(unpack(font))
		casttime:SetJustifyH('RIGHT')
		casttime:Hide()
		casttime.timer = function(elapsed)
			local self = casttime
			local min,max = self.min,self.max
			min = min + elapsed; self.min = min
			self:SetText(('%.1f'):format(max-min))
		end
		casttime:Listen('casting',function(self,name,_,startT,endT)
			if name then
				self:Show()
				self.min = GetTime() - startT
				self.max = endT - startT
				mod:timer_register(0,true,self.timer)(0)
			else
				self:Hide()
				self.min = nil
				self.max = nil
				mod:timer_unregister(self.timer)
			end
		end)
		target.casttime = casttime
